<?php

namespace App\Jobs\Stats\Retention\Day\Elasticsearch;

use App\Jobs\Stats\Retention\Day\Game as JobStats;
use App\Jobs\Stats\Traits\DatabaseElasticsearch;
use App\Models\Elasticsearch\Settlement\Info;

class Game extends JobStats
{
    use DatabaseElasticsearch;

    /**
     * 获取玩家ID列表
     *
     * @param int $timed
     * @param string $platCode
     * @return array
     */
    protected function getUserIds(int $timed, string $platCode): array
    {
        $params = ['body' => ['size' => 0, 'track_total_hits' => false]];
        // 筛选【时间】
        $this->makeWhereDatetime($params, 'end_time', [$timed, $timed + 86400], Info::INDEX_NAME);
        // 筛选【类型】
        $this->makeWhere($params, 'player_type', Info::PLAYER_TYPE);
        // 筛选【状态】
        $this->makeWhere($params, 'status', Info::STATUS_NORMAL);
        // 筛选【平台】
        $this->makeWhere($params, 'plat_code', $platCode);

        // 统计参数
        $params['body']['aggs'] = $this->makeGroup(['uid']);

        // 查询数据
        $buckets = Info::search($params)['aggregations']['group']['buckets'];

        return array_column(array_column($buckets, 'key'), 'uid');
    }

    /**
     * 获取玩家人数
     *
     * @param int $timed
     * @param array $ids
     * @return int
     */
    protected function getUserCount(int $timed, array $ids): int
    {
        $params = ['body' => ['size' => 0, 'track_total_hits' => false]];
        // 筛选【UID】
        $this->makeWhere($params, 'uid', $ids, 'terms');
        // 筛选【时间】
        $this->makeWhereDatetime($params, 'end_time', [$timed, $timed + 86400]);

        // 统计参数
        $params['body']['aggs'] = $this->makeDistinct('uid');

        // 查询数据
        return Info::search($params)['aggregations']['distinct_uid']['value'];
    }
}
